package com.blb.eduadservice.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 定时更新广告任务的配置
 */
@Slf4j
@Configuration
public class UpdateAdTaskConfig {

    @Scheduled(cron = "*/5 * * * * ?")
    public void updateAd() {
        try {
            //调用nginx中进行缓存更新的脚本
            URL url = new URL("http://localhost:7003/ad_update?space_id=1");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("Content-Type", "application/json");

            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            log.info("完成广告轮播图缓存更新{}",response.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
